package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.problem.*;
import com.mathlearning.auth.service.ProblemService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/problems")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    /**
     * 获取课程列表
     */
    @GetMapping("/courses")
    public ApiResponse<List<CourseResponse>> getCourses() {
        List<CourseResponse> courses = problemService.getCourses();
        return ApiResponse.success(courses);
    }

    /**
     * 获取题目列表
     */
    @GetMapping
    public ApiResponse<ProblemListResponse> getProblemList(
            ProblemListRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        ProblemListResponse response = problemService.getProblemList(request, userId);
        return ApiResponse.success(response);
    }

    /**
     * 获取题目详情
     */
    @GetMapping("/{id}")
    public ApiResponse<ProblemDetailResponse> getProblemDetail(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        ProblemDetailResponse response = problemService.getProblemDetail(id, userId);
        return ApiResponse.success(response);
    }

    /**
     * 提交答案
     */
    @PostMapping("/submit")
    public ApiResponse<SubmitResultResponse> submitAnswer(
            @Valid @RequestBody SubmitAnswerRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        SubmitResultResponse response = problemService.submitAnswer(userId, request);
        return ApiResponse.success("提交成功", response);
    }

    /**
     * 获取题解
     */
    @GetMapping("/{id}/solution")
    public ApiResponse<?> getSolution(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        ProblemDetailResponse.Example solution = problemService.getSolution(id, userId);
        return ApiResponse.success(solution);
    }

    /**
     * 获取题目答案和解答（无条件查看，用于学习）
     */
    @GetMapping("/{id}/answer")
    public ApiResponse<?> getAnswerAndSolution(@PathVariable Long id) {
        return problemService.getAnswerAndSolution(id);
    }

    /**
     * 收藏/取消收藏题目
     */
    @PostMapping("/{id}/favorite")
    public ApiResponse<FavoriteResponse> toggleFavorite(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        FavoriteResponse response = problemService.toggleFavorite(userId, id);
        String message = response.getIsFavorited() ? "收藏成功" : "取消收藏";
        return ApiResponse.success(message, response);
    }

    /**
     * 获取学习进度
     */
    @GetMapping("/progress")
    public ApiResponse<ProgressResponse> getProgress(HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        ProgressResponse response = problemService.getProgress(userId);
        return ApiResponse.success(response);
    }

    /**
     * 获取提交记录
     */
    @GetMapping("/submissions")
    public ApiResponse<SubmissionListResponse> getSubmissions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) String status,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        SubmissionListResponse response = problemService.getSubmissions(userId, page, pageSize, status);
        return ApiResponse.success(response);
    }

    /**
     * 获取随机每日推荐题目
     * GET /api/problems/daily-random?difficulty=medium
     */
    @GetMapping("/daily-random")
    public ApiResponse<DailyProblemResponse> getDailyRandomProblem(
            @RequestParam(required = false) String difficulty,
            HttpServletRequest httpRequest) {
        // 获取用户ID（可能为null，未登录用户也可以获取推荐）
        Long userId = (Long) httpRequest.getAttribute("userId");
        
        DailyProblemResponse problem = problemService.getRandomDailyProblem(userId, difficulty);
        
        if (problem == null) {
            return ApiResponse.success("暂无题目", null);
        }
        
        return ApiResponse.success("获取成功", problem);
    }
}



